(* The MIT License

Copyright (c) 2010 Shunsuke Okamoto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. *)

on _error(num, msg)
	tell me
		activate
		error msg number num
	end tell
end _error

on get_front_window()
	(*
	Get front window, it contained document. Filtered other windows.
		input: none
		output: reference of front document, or missing value
	*)
	tell application "Safari"
		if not (front document exists) then return missing value
		set the_document to front document
		set window_ref to a reference to (windows whose document of it is not missing value)
		repeat with this_window in window_ref
			if document of this_window is the_document then return contents of this_window
		end repeat
		return missing value
	end tell
end get_front_window

on get_document_windows()
	(*
	Get windows, these contained document.
		input: none
		output: list of document windows. If not exists windows then return empty list.
	*)
	tell application "Safari"
		if not (documents exists) then return {}
		return (windows whose document of it is not missing value)
	end tell
	return {}
end get_document_windows

on close_tabs(this_window)
	(*
	close all tabs, without current tab.
		input: document window reference
		output: none
	*)
	tell application "Safari"
		set front_window to my get_front_window()
		if front_window is missing value then return
		set current_tab to current tab of front_window
		close (tabs of front_window whose it is not current_tab)
	end tell
end close_tabs

on close_document_windows()
	(*
	close all document windows, without front document window.
		ipnut: none
		output: none
	*)
	tell application "Safari"
		set front_window to my get_front_window()
		if front_window is missing value then return
		set window_list to my get_document_windows()
		if window_list is {} then return
		repeat with this_window in window_list
			if this_window is not front_window then close this_window
		end repeat
	end tell
end close_document_windows

on get_selected_text(this_window)
	(*
	Get specifier window's selected text.
		input: document window reference
		output: text. If can't get selected text then return empty text.
	*)
	
	if not (my enable_javascript()) then
		my _error(100, "You should enable JavaScript")
	end if
	
	if not (document of this_window exists) then
		my _error(200, "Argument error. You should pass to document window reference.")
	end if
	
	tell application "Safari"
		set selected_text_list to {}
		tell current tab of this_window
			set end of selected_text_list to do JavaScript "getSelection();" in it
			
			set frame_length to do JavaScript "parent.frames.length;" in it
			set num to 0
			repeat while num < frame_length
				set script_text to "parent.frames[" & num & "].getSelection();" as text
				set end of selected_text_list to do JavaScript script_text in it
				set num to num + 1
			end repeat
		end tell
		return selected_text_list as text
	end tell
end get_selected_text

on mail_link(this_window)
	(*
	Mail to specifier window. This handler is Safari's `Mail Link to This Page` menu.
		input: document window reference
		output: none
	*)
	tell application "Safari"
		tell current tab of this_window
			set URL to "mail-link:"
		end tell
	end tell
end mail_link

on mail_contents(this_window)
	(*
	Mail to specifier window. This handler is Safari's `Mail Contents to This Page` menu.
		input: document window reference
		output: none
	*)
	tell application "Safari"
		tell this_window
			email contents of current tab
		end tell
	end tell
end mail_contents

on screen_size(this_window)
	(*
	Get main display's screen size.
		input: document window reference
		output: list
	*)
	tell application "Safari"
		tell current tab of this_window
			return do JavaScript "[screen.availWidth,screen.availHeight]" in it
		end tell
	end tell
end screen_size

on set_window_size(this_window, {w, h})
	(*
	Set size of specifier window's.
		input: document window reference, list of target size. w = width, h = height.
		output: none
	*)
	set script_code to "self.moveTo(0,0);
self.resizeTo(" & (w as text) & "," & (h as text) & ");"
	tell application "Safari"
		tell current tab of this_window
			do JavaScript script_code in it
		end tell
	end tell
end set_window_size

on full_screen(this_window)
	(*
	Full screen specifier window.
		input: document window reference
		output: none
	*)
	set {w, h} to my screen_size(this_window)
	my set_window_size(this_window, {w, h})
end full_screen

on enable_javascript()
	(*
	Enable or not JavaScript.
		input: none
		output: boolean
	*)
	set front_window to my get_front_window()
	if front_window is missing value then return false
	tell application "Safari"
		tell current tab of front_window
			try
				set the_date to do JavaScript "new Date();" in it
				the_date
				return true
			on error msg number num
				if num = -2753 then
					return false
				else
					return {num, msg}
				end if
			end try
		end tell
		return false
	end tell
end enable_javascript

on run
	return me
end run